import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.Scanner;

import org.junit.Test;

import jdk.tools.jlink.resources.plugins;

//import jdk.nashorn.internal.ir.BinaryNode;

public class MyTest {
    private static class BinaryTree{
        int element;
        BinaryTree leftChild;
        BinaryTree rightChild;
        public BinaryTree(int element){
            this.element=element;
        }
        public BinaryTree(){}
        public BinaryTree build(int[] elements){
            BinaryTree[] binaryTrees=new BinaryTree[elements.length];
            for(int i=0;i<elements.length;i++){
                binaryTrees[i]=new BinaryTree(i);
            }
            int idx=1;
            ConcurrentLinkedQueue<BinaryTree> priorityQueue=new ConcurrentLinkedQueue<BinaryTree>();
            priorityQueue.add(binaryTrees[0]);
            while(true){
                BinaryTree binaryTree=priorityQueue.poll();
                binaryTree.leftChild=binaryTrees[idx];
                priorityQueue.add(binaryTrees[idx]);
                idx++;
                if(idx==elements.length){
                    break;
                }
                binaryTree.rightChild=binaryTrees[idx];
                priorityQueue.add(binaryTrees[idx]);
                idx++;
                if(idx==elements.length){
                    break;
                }
            }
            return binaryTrees[0];
        } 
    }
    private static class BinaryTreeReader{
        public static BinaryTreeReader binaryTreeReader;
        private BinaryTreeReader(){

        }
        public static BinaryTreeReader getBinaryTreeReader(){
            if(binaryTreeReader==null){
                binaryTreeReader=new BinaryTreeReader();
            }
            return binaryTreeReader;
        }
        public void DLR(BinaryTree root){
            if(root==null){
                return;
            }
            DLR(root.leftChild);
            DLR(root.rightChild);
            System.out.print(root.element+" ");
        }
        public void LDR(BinaryTree root){
            if(root==null){
                return;
            }
            LDR(root.leftChild);
            System.out.print(root.element+" ");
            LDR(root.rightChild);
        }
        public void VLR(BinaryTree root){
            if(root==null){
                return;
            }
            System.out.print(root.element+" ");
            VLR(root.leftChild);
            VLR(root.rightChild);
        }
    
    }




    @Test
    public void test1(){
        int[] arr={0,1,2,3,4};
        BinaryTree root=new BinaryTree();
        root=root.build(arr);
        BinaryTreeReader binaryTreeReader=BinaryTreeReader.getBinaryTreeReader();
        binaryTreeReader.DLR(root);
        System.out.println("");
        binaryTreeReader.LDR(root);
        System.out.println("");
        binaryTreeReader.VLR(root);
    }


}
